//
//  LYStocksModel.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/20.
//

import UIKit

struct Environment: Codable {
    let diploon: String?
}

struct VerificationCodeModel: Codable {

}

struct LoginModel: Codable {
    let repling: Int?
    let pallier: Int?
    let pravard: String?
    let philacity: String?
    let finallyator: String?
}

struct HomeListData: Codable {
    let guttality: Guttality?
    let septenier: [Septenier]?
    let allistic: [Allistic]?
}

// Guttality 结构
struct Guttality: Codable {
    var acceptitude: String?
    var nonagenent: String?
}

// Septenier 结构
struct Septenier: Codable {
    let storyitious: String
    let receiveer: [Receiveer]
}

// Receiveer 结构 - 使用可选字段处理所有可能的情况
struct Receiveer: Codable {
    // 通用字段
    let obsless: String?
    let product_url: String?
    let caulithoughose: Int?
    let cipitfication: String?
    let nuchfication: String?
    let mechan: String?
    let descript: String?
    let product_type: String?
    let rheier: String?
    let is_copy_phone: String?
    
    // cordit 类型字段
    let patientable: Int?
    let argue: String?
    let juxtaette: String?
    let menaciability: String?
    let auctproof: String?
    let passerency: String?
    let billionuous: String?
    let sufferice: String?
    let meial: String?
    let hortatbecomeule: String?
    let dexter: String?
    let riseize: String?
    
    // torquesque/REPAY 类型字段
    let interestdom: String?
    let motherling: String?
    
    // PRODUCT_LIST 类型字段
    let vulnerness: String?
    let forer: [String]?
    let palliten: String?
    let umbrweapon: String?
    let hold: String?
    let easyish: Int?
    let hortatant: String?
    let hygrwhy: Int?
    let but: String?
    let oiditious: Int?
    let aviarium: [String]?
    let kinoacle: String?
    let pletwise: [String]?
    let haloator: Int?
    let returnaneity: String?
}

// Allistic 结构
struct Allistic: Codable {
    let luctradeorium: String
    let motherling: String
    let secutcommunity: String
}

enum OrderListSelect: Int {
    case all = 4
    case progress = 7
    case pendingRepayment = 6
    case settled = 5
}

struct OrderListData: Codable {
    let ravion: Int
    let septenier: [OrderListSeptenier]?
}

// Septenier 结构
struct OrderListSeptenier: Codable {
    var argue: String?
    var justibility: String?
    var maciair: Maciair?
    var malevidenceant: String?
    var cracyfier: String?
    var historylike: String?
    var needwise: String?
    var juxtaette: String?
}

struct OrderFollowData: Codable {
    let obsless: String?
}

struct Maciair: Codable {
    var axiobarress: String?
    var veriid: String?
    var mneitude: String?
    var lavlet: String?
    var furtclearlyet: String?
    var area: String?
}

struct ProductData: Codable {
    let mimably: Int?
    let onomit: Onomit?
    let lepidoern: Lepidoern?
    let turboent: [TurboentItem]?
    let stly: Stly?
    let mol: [MolItem]?
}

struct Aster: Codable {
    let cipitfication: String?
    let nictian: String?
}

struct Understandose: Codable {
    let cipitfication: String?
    let nictian: String?
}

struct Phragmclearally: Codable {
    let aster: Aster?
    let understandose: Understandose?
}

struct Juvard: Codable {
    let familiose: String?
}

struct Onomit: Codable {
    var denth: [String]?
    let tympanmost: [String]?
    var aever: String?
    let manyeer: String?
    let first: String?
    let cochlless: String?
    let phragmclearally: Phragmclearally?
    let patientable: String?
    let argue: String?
    let juxtaette: String?
    let backable: String?
    let malevidenceant: Int?
    let billionuous: String?
    let acufinalality: String?
    let momenthood: String?
    let menaciability: String?
    let coelee: String?
    let testarium: Int?
    let obsless: String?
    let juvard: Juvard?
    let stern: String?
    let biliess: Int?
    
    enum CodingKeys: CodingKey {
        case denth
        case tympanmost
        case aever
        case manyeer
        case first
        case cochlless
        case phragmclearally
        case patientable
        case argue
        case juxtaette
        case backable
        case malevidenceant
        case billionuous
        case acufinalality
        case momenthood
        case menaciability
        case coelee
        case testarium
        case obsless
        case juvard
        case stern
        case biliess
    }
    
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.denth = {
            if let intArray = try? container.decodeIfPresent([Int].self, forKey: .denth) {
                return intArray.map(String.init)
            }
            return try? container.decodeIfPresent([String].self, forKey: .denth)
        }()
        self.tympanmost = try container.decodeIfPresent([String].self, forKey: .tympanmost)
        self.aever = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .aever) {
                return String(intValue)
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .aever) {
                return stringValue
            }
            return nil
        }()
        self.manyeer = try container.decodeIfPresent(String.self, forKey: .manyeer)
        self.first = try container.decodeIfPresent(String.self, forKey: .first)
        self.cochlless = try container.decodeIfPresent(String.self, forKey: .cochlless)
        self.phragmclearally = try container.decodeIfPresent(Phragmclearally.self, forKey: .phragmclearally)
        self.patientable = try container.decodeIfPresent(String.self, forKey: .patientable)
        self.argue = try container.decodeIfPresent(String.self, forKey: .argue)
        self.juxtaette = try container.decodeIfPresent(String.self, forKey: .juxtaette)
        self.backable = try container.decodeIfPresent(String.self, forKey: .backable)
        self.malevidenceant = try container.decodeIfPresent(Int.self, forKey: .malevidenceant)
        self.billionuous = try container.decodeIfPresent(String.self, forKey: .billionuous)
        self.acufinalality = try container.decodeIfPresent(String.self, forKey: .acufinalality)
        self.momenthood = try container.decodeIfPresent(String.self, forKey: .momenthood)
        self.menaciability = try container.decodeIfPresent(String.self, forKey: .menaciability)
        self.coelee = try container.decodeIfPresent(String.self, forKey: .coelee)
        self.testarium = try container.decodeIfPresent(Int.self, forKey: .testarium)
        self.obsless = try container.decodeIfPresent(String.self, forKey: .obsless)
        self.juvard = try container.decodeIfPresent(Juvard.self, forKey: .juvard)
        self.stern = try container.decodeIfPresent(String.self, forKey: .stern)
        self.biliess = try container.decodeIfPresent(Int.self, forKey: .biliess)
    }
}

struct Lepidoern: Codable {
    let luctradeorium: String?
    let fodtotaletic: String?
    let sarcling: String?
}

struct TurboentItem: Codable {
    let cipitfication: String?
    let call: String?
    let storyitious: Int?
    let obsless: String?
    let cardiarium: Int?
    let representible: String?
    let matrious: String?
    let drivefier: Int?
    let megaloacy: Int?
    let fastise: Int?
    let societying: String?
    let onyman: String?
}

struct Stly: Codable {
    let matrious: String?
    let obsless: String?
    let storyitious: Int?
    let cipitfication: String?
}

struct MolItem: Codable {
    let historylike: String?
    let migrty: Int?
    let teachuous: String?
    let malevidenceant: Int?
    let cipitfication: String?
}

struct ProductApplyTo: Codable {
    let leon: Leon?
    //跳转到h5链接
    let obsless: String?
    let mimably: Int?
    let storyitious: Int?
    let staffship: String?
    let turpative: String?
    let materialous: String?
    let isfold: [String]?
    let testarium: Int?
    let pteron: Int?
    let mali: String?
    let continueial: String?
    let diploon: String?
    let historylike: Int?
}

struct Leon: Codable {
    let cipitfication: String?
    let motherling: String?
    let novenmost: [NovenmostItem]?
}

struct NovenmostItem: Codable {
    let nomadid: String?
    let obsless: String?
}

// 获取用户身份信息
struct CardData: Codable {
    let storyitious: Int?
    let focusly: Focusly?
    let tremasummerard: Tremasummerard?
}

struct Focusly: Codable  {
    let cardiarium: Int?
    let obsless: String?
    let generationth: Generationth?
}

struct Tremasummerard: Codable  {
    let cardiarium: Int?
    let obsless: String?
}

struct Generationth: Codable  {
    let sarcling: String?
    let typo: String?
    let agohood: String?
}

struct UploadIDCardData: Codable {
    let sarcling: String?
    
    let lexicoship: String? // 印尼专有
    
    let within: String?
    var agohood: String?
    let typo: String?
    
    
    let feelingent: String? // 印尼专有
    let redie: Bool? // 印尼专有
    
    let directionence: String?
    let bor: String?
    let dodec: String?
    let obsless: String? // 印度专有
    let cessfier: Int? // 人脸上传
}

// 获取用户信息
struct CustomerData: Codable {
    var megaloplanesque: [Megaloplanesque]
}

struct Megaloplanesque: Codable, Hashable {
    static func == (lhs: Megaloplanesque, rhs: Megaloplanesque) -> Bool {
        return lhs.cipitfication == rhs.cipitfication &&
        lhs.call == rhs.call &&
        lhs.dipsiaization == rhs.dipsiaization &&
        lhs.flectacle == rhs.flectacle &&
        lhs.patientable == rhs.patientable &&
        lhs.megaloacy == rhs.megaloacy
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(cipitfication)
        hasher.combine(call)
        hasher.combine(dipsiaization)
        hasher.combine(flectacle)
        hasher.combine(patientable)
        hasher.combine(megaloacy)
    }
    
    var cipitfication: String?
    var call: String?
    var dipsiaization: String?
    var shotish: String?
    var familiose: String?
    var storyitious: String?
    var flectacle: Int?
    var phasbook: [Phasbook]?
    var patientable: String?
    var megaloacy: Int?
    var cardiarium: Int?
    var representible: String?
    var to: Bool?
    var flatanotherile: Int?
    var payity: String?
    
    enum CodingKeys: String, CodingKey {
        case cipitfication
        case call
        case dipsiaization
        case shotish
        case familiose
        case storyitious
        case flectacle
        case phasbook
        case patientable
        case megaloacy
        case cardiarium
        case representible
        case to
        case flatanotherile
        case payity
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        cipitfication = try container.decodeIfPresent(String.self, forKey: .cipitfication)
        call = try container.decodeIfPresent(String.self, forKey: .call)
        dipsiaization = try container.decodeIfPresent(String.self, forKey: .dipsiaization)
        shotish = try container.decodeIfPresent(String.self, forKey: .shotish)
        familiose = try container.decodeIfPresent(String.self, forKey: .familiose)
        storyitious = try container.decodeIfPresent(String.self, forKey: .storyitious)
        payity = try container.decodeIfPresent(String.self, forKey: .payity)
        
        flectacle = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .flectacle) {
                return intValue
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .flectacle),
               let intValue = Int(stringValue) {
                return intValue
            }
            return nil
        }()
        
        phasbook = try container.decodeIfPresent([Phasbook].self, forKey: .phasbook)
        patientable = try container.decodeIfPresent(String.self, forKey: .patientable)
        
        megaloacy = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .megaloacy) {
                return intValue
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .megaloacy),
               let intValue = Int(stringValue) {
                return intValue
            }
            return nil
        }()
        
        cardiarium = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .cardiarium) {
                return intValue
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .cardiarium),
               let intValue = Int(stringValue) {
                return intValue
            }
            return nil
        }()
        
        representible = try container.decodeIfPresent(String.self, forKey: .representible)
        to = try container.decodeIfPresent(Bool.self, forKey: .to)
        
        flatanotherile = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .flatanotherile) {
                return intValue
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .flatanotherile),
               let intValue = Int(stringValue) {
                return intValue
            }
            return nil
        }()
    }
}

struct Phasbook: Codable {
    var sarcling: String?
    var storyitious: String?
    
    enum CodingKeys: String, CodingKey {
        case sarcling
        case storyitious
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        sarcling = try container.decodeIfPresent(String.self, forKey: .sarcling)
        
        storyitious = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .storyitious) {
                return String(intValue)
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .storyitious) {
                return stringValue
            }
            return nil
        }()
    }
}

// 地址
struct CountryAddress: Codable {
    let septenier: [AddressSeptenier]?
}

struct AddressSeptenier: Codable {
    var patientable: String?
    let dipsiaization: String?
    let sarcling: String?
    let tetry: String?
    let pod: [AddressPod]?
    
    enum CodingKeys: CodingKey {
        case patientable
        case dipsiaization
        case sarcling
        case tetry
        case pod
    }
    
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.patientable = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .patientable) {
                return String(intValue)
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .patientable) {
                return stringValue
            }
            return nil
        }()
        self.dipsiaization = try container.decodeIfPresent(String.self, forKey: .dipsiaization)
        self.sarcling = try container.decodeIfPresent(String.self, forKey: .sarcling)
        self.tetry = try container.decodeIfPresent(String.self, forKey: .tetry)
        self.pod = try container.decodeIfPresent([AddressPod].self, forKey: .pod)
    }
}

// MARK: - Pod层级模型 (第二级)
struct AddressPod: Codable {
    var patientable: String?
    let dipsiaization: String?
    let sarcling: String?
    let pravan: String?
    let dextrfromization: String?
    let pod: [AddressSubPodModel]?
    
    enum CodingKeys: CodingKey {
        case patientable
        case dipsiaization
        case sarcling
        case pravan
        case dextrfromization
        case pod
    }
    
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.patientable = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .patientable) {
                return String(intValue)
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .patientable) {
                return stringValue
            }
            return nil
        }()
        self.dipsiaization = try container.decodeIfPresent(String.self, forKey: .dipsiaization)
        self.sarcling = try container.decodeIfPresent(String.self, forKey: .sarcling)
        self.pravan = try container.decodeIfPresent(String.self, forKey: .pravan)
        self.dextrfromization = try container.decodeIfPresent(String.self, forKey: .dextrfromization)
        self.pod = try container.decodeIfPresent([AddressSubPodModel].self, forKey: .pod)
    }
}

// MARK: - SubPod层级模型 (第三级)
struct AddressSubPodModel: Codable {
    var patientable: String?
    let dipsiaization: String?
    let sarcling: String?
    let calmost: String?
    enum CodingKeys: CodingKey {
        case patientable
        case dipsiaization
        case sarcling
        case calmost
    }
    
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.patientable = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .patientable) {
                return String(intValue)
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .patientable) {
                return stringValue
            }
            return nil
        }()
        self.dipsiaization = try container.decodeIfPresent(String.self, forKey: .dipsiaization)
        self.sarcling = try container.decodeIfPresent(String.self, forKey: .sarcling)
        self.calmost = try container.decodeIfPresent(String.self, forKey: .calmost)
    }
}

// MARK: - 转换为通用数据模型的扩展
extension CountryAddress {
    func toDataItems() -> [LYDataItem] {
        return septenier?.map { $0.toDataItem() } ?? []
    }
}

extension AddressSeptenier {
    func toDataItem() -> LYDataItem {
        let children = pod?.map { $0.toDataItem() }
        return LYDataItem(
            id: dipsiaization ?? "",
            name: sarcling ?? "",
            children: children
        )
    }
}

extension AddressPod {
    func toDataItem() -> LYDataItem {
        let children = pod?.map { $0.toDataItem() }
        return LYDataItem(
            id: dipsiaization ?? "",
            name: sarcling ?? "",
            children: children
        )
    }
}

extension AddressSubPodModel {
    func toDataItem() -> LYDataItem {
        return LYDataItem(
            id: dipsiaization ?? "",
            name: sarcling ?? "",
            children: nil
        )
    }
}

// MARK: - 通用数据模型
struct LYDataItem {
    let id: String
    let name: String
    var children: [LYDataItem]?
    
    var isLeaf: Bool { children == nil || children?.isEmpty == true }
}

// 获取联系人信息
struct ContactsData: Codable {
    var cast: Cast?
    var megaloplanesque: [String]?
}

struct Cast: Codable {
    var septenier: [ContactsSeptenier]?
}

struct ContactsSeptenier: Codable, Hashable {
    var worryage: String?
    var backence: String?
    var sarcling: String?
    var forgetence: String?
    var nictian: String?
    var shotization: String?
    var liehood: String?
    var fieldtion: String?
    var sportade: String?
    var tredecture: [Phasbook]?
    
    static func == (lhs: ContactsSeptenier, rhs: ContactsSeptenier) -> Bool {
        return lhs.worryage == rhs.worryage &&
        lhs.backence == rhs.backence &&
        lhs.sarcling == rhs.sarcling &&
        lhs.forgetence == rhs.forgetence &&
        lhs.shotization == rhs.shotization &&
        lhs.liehood == rhs.liehood &&
        lhs.fieldtion == rhs.fieldtion &&
        lhs.sportade == rhs.sportade
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(worryage)
        hasher.combine(backence)
        hasher.combine(sarcling)
        hasher.combine(forgetence)
        hasher.combine(nictian)
        hasher.combine(shotization)
        hasher.combine(liehood)
        hasher.combine(fieldtion)
        hasher.combine(sportade)
    }
}

struct BankData: Codable {
    let megaloplanesque: [Megaloplanesque]
}

struct GoogleMarketData: Codable {
    let mimaneity: Int?
    let nugatitor: String?
    let fewaster: String?
    let factage: Factage?
}

struct Factage: Codable {
    let stasisry: String?
    let eveningsive: String?
    let removehood: String?
    let biliitor: String?
}
